package com.goods.business.service.imp;

import com.goods.business.mapper.SupplierMapper;
import com.goods.business.service.SupplierService;
import com.goods.common.model.business.Supplier;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.SupplierVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SupplierServiceImpl implements SupplierService {

    @Autowired
    private SupplierMapper supplierMapper;

    //加载系别列表
    @Override
    public PageVO<SupplierVO> getSupplierList(Integer pageNum, Integer pageSize, String name) {
        /*if (!StringUtils.isEmpty(name)){
            //name不为空
            Example example = new Example(Consumer.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("name",name);
            ConsumerVO consumerVO = consumerMapper.selectOneByExample(example);
            PageVO pageVO = new PageVO(,);
        }else {
            List<ConsumerVO> consumerVOS = consumerMapper.selectAll();
        }*/
        List<Supplier> supplierList = supplierMapper.selectAll();
        List<SupplierVO> supplierVOList = supplierList.stream().map(consumer -> {
            SupplierVO supplierVO = new SupplierVO();
            BeanUtils.copyProperties(consumer,supplierVO);
            return supplierVO;
        }).collect(Collectors.toList());
        List<SupplierVO> page = ListPageUtils.page(supplierVOList, pageSize, pageNum);
        PageVO pageVO = new PageVO(supplierVOList.size(),page);
        return pageVO;
    }

    //添加
    @Override
    public void add(SupplierVO supplierVO) {
        Supplier supplier = new Supplier();
        BeanUtils.copyProperties(supplierVO,supplier);
        Date time = Calendar.getInstance().getTime();
        supplier.setCreateTime(time);
        supplier.setModifiedTime(time);
        supplierMapper.insert(supplier);
    }

    //打开编辑弹窗
    @Override
    public SupplierVO edit(Long id) {
        Supplier supplier = supplierMapper.selectByPrimaryKey(id);
        System.out.println("supplier = " + supplier);
        SupplierVO supplierVO = new SupplierVO();
        BeanUtils.copyProperties(supplier,supplierVO);
        return supplierVO;
    }

    //更新修改   先查询 在删除 在添加
    @Override
    public void update(Long id, SupplierVO supplierVO) {
        supplierMapper.deleteByPrimaryKey(id);
        Supplier supplier = new Supplier();
        BeanUtils.copyProperties(supplierVO,supplier);
        supplier.setId(id);
        supplierMapper.insert(supplier);
    }

    //删除
    @Override
    public void delete(Long id) {
        supplierMapper.deleteByPrimaryKey(id);
    }
}
